package com.ruoyi.wms.controller;

import java.util.List;

import com.ruoyi.main.service.IMainUnitesService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.wms.domain.WmsProductIncome;
import com.ruoyi.wms.service.IWmsProductIncomeService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 产品入库Controller
 * 
 * @author chao
 * @date 2024-07-24
 */
@Controller
@RequestMapping("/wms/income")
public class WmsProductIncomeController extends BaseController
{
    private String prefix = "wms/income";

    @Autowired
    private IWmsProductIncomeService wmsProductIncomeService;

    @Autowired
    private IMainUnitesService mainUnitesService;

    @RequiresPermissions("wms:income:view")
    @GetMapping()
    public String income()
    {
        return prefix + "/income";
    }

    /**
     * 查询产品入库列表
     */
    @RequiresPermissions("wms:income:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(WmsProductIncome wmsProductIncome)
    {
        startPage();
        List<WmsProductIncome> list = wmsProductIncomeService.selectWmsProductIncomeList(wmsProductIncome);
        return getDataTable(list);
    }

    /**
     * 导出产品入库列表
     */
    @RequiresPermissions("wms:income:export")
    @Log(title = "产品入库", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(WmsProductIncome wmsProductIncome)
    {
        List<WmsProductIncome> list = wmsProductIncomeService.selectWmsProductIncomeList(wmsProductIncome);
        ExcelUtil<WmsProductIncome> util = new ExcelUtil<WmsProductIncome>(WmsProductIncome.class);
        return util.exportExcel(list, "产品入库数据");
    }

    /**
     * 新增产品入库
     */
    @GetMapping("/add")
    public String add(Model model)
    {
        model.addAttribute("uniteNames",mainUnitesService.getAllNormalNames());
        return prefix + "/add";
    }

    /**
     * 新增保存产品入库
     */
    @RequiresPermissions("wms:income:add")
    @Log(title = "产品入库", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(WmsProductIncome wmsProductIncome)
    {
        return toAjax(wmsProductIncomeService.insertWmsProductIncome(wmsProductIncome));
    }

    /**
     * 修改产品入库
     */
    @RequiresPermissions("wms:income:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        WmsProductIncome wmsProductIncome = wmsProductIncomeService.selectWmsProductIncomeById(id);
        mmap.put("wmsProductIncome", wmsProductIncome);
        mmap.put("uniteNames",mainUnitesService.getAllNormalNames());
        return prefix + "/edit";
    }

    /**
     * 修改保存产品入库
     */
    @RequiresPermissions("wms:income:edit")
    @Log(title = "产品入库", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(WmsProductIncome wmsProductIncome,Model model)
    {
        model.addAttribute("uniteNames",mainUnitesService.getAllNormalNames());
        return toAjax(wmsProductIncomeService.updateWmsProductIncome(wmsProductIncome));
    }

    /**
     * 删除产品入库
     */
    @RequiresPermissions("wms:income:remove")
    @Log(title = "产品入库", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(wmsProductIncomeService.deleteWmsProductIncomeByIds(ids));
    }


    /**
     * 执行产品入库
     */
    @Log(title = "执行入库", businessType = BusinessType.OTHER)
    @PostMapping("/executeProductStorage")
    @ResponseBody
    public AjaxResult Productstorge(@RequestParam("RowId") Long RowId, @RequestParam("ProductId") Long ProductId, @RequestParam("num") Double num){
        try {
            wmsProductIncomeService.Productstorge(RowId,ProductId,num);
            return AjaxResult.success("入库成功");
        } catch (Exception e) {
            return AjaxResult.error("入库失败：" + e.getMessage());
        }
    }


}
